
/**
 * @file pdtmanagement/Home.js
 *  产品管理
 * @author
 */

import React, {Component,PropTypes} from 'react';
import {connect} from 'dva';
import {routerRedux,Link,withRouter } from 'dva/router';
import { Button,Modal,Row,Col,Table,Icon,Pagination,Upload,Card,Menu, Dropdown} from 'antd';
import Tabtitle from '../../../components/common/TabTitle';
import Pdtadd from './components/Addpdt';
import Search from '../../../components/common/Search';
import DropOption from '../../../components/common/DropOption';
import Uploadfile from './Uploadfiles';
import styles from './home.less';
import Tablelist from '../../../components/common/TableData';


let obj ={};
let productId;
const IDprops = {};
const confirm = Modal.confirm;
const Pdtmanagement = ({dispatch,pdtmanagement,app,location:{
      pathname,
      query
    }}) => {
	if(pdtmanagement.status){
		const { pdtlist,page,state} = pdtmanagement;
		console.log("pdtmanagement>>>>>>",pdtmanagement,'---',location)
		// const name = pdtmanagement.name
		const handleMenuClick = (record, e) => {
			if (e.key === '1') {
				dispatch({
					type: 'pdtmanagement/gotoViweposition',
					payload: {
					 	productId:record.productId,
					 	productCode:record.productCode
					}
				})
			}
			else if(e.key === '2'){
				dispatch({
					type: 'pdtmanagement/gotoViwetrade',
					payload: {
					 	productId:record.productId,
					 	productCode:record.productCode
					}
				})
			}
			else if (e.key === '4') {
				deleteitem(record)
			}
		}

		const columns = [
			{
				title: '产品Code',
				dataIndex: 'productCode',
				key: 'productCode',
			},
			{
				title: '产品名称',
				dataIndex: 'productName',
				key: 'productName',
			},
			{
				title: '产品类型',
				dataIndex: 'productTypeText',
				key: 'productTypeText',
			},
			{
				title: '产品类别',
				dataIndex: 'origProductTypeText',
				key: 'origProductTypeText',
			},
			{
				title: '生效时间',
				dataIndex: 'effectiveDate',
				key: 'effectiveDate',
			},

			{
				title: '操作',
				key: 'action',
				render:(record)=>{
						IDprops.productId = record.productId;
						IDprops.effectiveDate = record.effectiveDate;
						IDprops.sidebarFold = app.siderFold;
						// const IDprops ={
						// 	productId:record.productId,
						// 	effectiveDate:record.effectiveDate,
						// 	sidebarFold:app.siderFold
						// }
						function handleTorevise(){
							dispatch(routerRedux.push({
								pathname:'/systemMgt/accountMgt/pdtMgt/revise',
								query:{productId:record.productId}
							}))
						}
						const menu = (
						  <Menu>
						    <Menu.Item>
						    	<Uploadfile {...IDprops} name="按数量"/>
						    </Menu.Item>
						    <Menu.Item>
						    	<Uploadfile {...IDprops} name="按权重"/>
						    </Menu.Item>
						    <Menu.Item>
						    	<Uploadfile {...IDprops} name="按产品估值"/>
						    </Menu.Item>
						  </Menu>
						);
						return (
							<span>
								<span onClick={handleTorevise}><Link>编辑</Link></span>&nbsp;&nbsp;&nbsp;
								<Dropdown overlay={menu}>
						      <Button className={styles.dropdownlink} >
							      导入持仓 <Icon type="down" />
							    </Button>
							  </Dropdown>&nbsp;&nbsp;
								<DropOption 
									onMenuClick={e => handleMenuClick(record, e)} 
									menuOptions={[{key:'1',name:'查看持仓'},{key: '2', name: '查看交易'},{ key: '4', name: '删除' }]} />
							</span>
						)
				}
			},
		];

		function deleteitem(record) {
			obj.productId = record.productId;
			confirm({
				title: '您确定要删除此条数据?',
				cancelText:"取消",
				onOk() {
					dispatch({
						type: 'pdtmanagement/removePdt',
						payload: obj
					})
				},
				onCancel() {
					console.log('Cancel');
				},
			});
		}

		// pagesize
		function onShowSizeChange(current, pagesize) {
			if(location.href.indexOf('name')>-1){
				const pathArr = location.href.split('&')[1]
				const name = decodeURI(pathArr.split('=')[1])
				dispatch({
					type: 'pdtmanagement/getPdtList',
					payload: {
						currentpage:1,
						pagesize,
						name:name
					}
				})
			}else{
				dispatch({
					type: 'pdtmanagement/getPdtList',
					payload: {
						currentpage:1,
						pagesize,
					}
				})
			}
			
		}
		//current
		function onChangecurrentpage(current,pagesize){
			if(location.href.indexOf('name')>-1){
				const pathArr = location.href.split('&')[1]
				const name = decodeURI(pathArr.split('=')[1])
				dispatch({
					type: 'pdtmanagement/getPdtList',
					payload: {
						currentpage:current,
						pagesize:pagesize,
						name:name
					}
				})
			}else{
				dispatch({
					type: 'pdtmanagement/getPdtList',
					payload: {
						currentpage:current,
						pagesize:pagesize,
					}
				})
			}
		}
		function onFormQuery(Values) {
			const name = Values.values
			if(name){
				dispatch({
					type:'pdtmanagement/changeRoute',
					payload:{
						pagesize:15,
						currentpage:1,
						name:name.keyname || ''
					}
				})
				dispatch({
					type:'pdtmanagement/initData',
					payload:{
						pagesize:15,
						currentpage:1,
						name:name.keyname
					}
				})
			}else{
				if(name == undefined){
					dispatch({
					type:'pdtmanagement/changeRoute',
					payload:{
						pagesize:15,
						currentpage:1,
						name:''
					}
				})
				}
			}
		}
		return (
				<div>
				<Tabtitle value={'产品管理'}/>
					<div className="page-pdtmanagement-home content-inner">
						<div className={styles.searchcolumn}>
							<Row  type="flex" justify="space-between" align="middle">
								<Col className={styles.searchbtn}>
									<Search
										buttonText="查询"
										onQuery={onFormQuery}
									/>
								</Col>
								<Col span={2} pull={2}>
									<Pdtadd className={styles.addbtn}/>
								</Col>
							</Row>
						</div>
            <Card>
              <Tablelist
  							columns={columns}
  							data={pdtlist}
  							page={page}
  							onShowSizeChange={onShowSizeChange}
  							onChangecurrentpage={onChangecurrentpage}
  							record={record => record.productId}
  						/>
            </Card>
				</div>
			</div>
		)
	}else{
		return null
	}
}

Pdtmanagement.propTypes = {
}

export default connect(({pdtmanagement,app})=>({pdtmanagement,app}))(Pdtmanagement)
